﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Ch.Admin.Bfs.Sms.Sdmx.Extension.CodeBook.Meta;


namespace bfs.sms.sdmx.util.Control
{
    public partial class UcGenerateOptions : UserControl
    {
        public UcGenerateOptions()
        {
            InitializeComponent();
            InitControls(null);

            // Mode  Not yet supported
            comboBoxWsMode.SelectedIndex = 1;
            comboBoxWsMode.Enabled = false;
        }

        private void InitControls(VarCatalogOptions opt)
        {
            if (opt == null)
                opt = new VarCatalogOptions();

            comboBoxQualityMode.SelectedIndex = (int)opt.Quality;
            numericUpDownVarIdPix.Value = (decimal)opt.VarIdPixels;
            numericUpDownVarNamePix.Value = (decimal)opt.VarNamePixels;
            numericUpDownCodeIdPix.Value = (decimal)opt.CodeIdPixels;
            numericUpDownCodeNamePix.Value = (decimal)opt.CodeNamePixels;
        }

        public VarCatalogOptions CatalogOptions
        {
            get
            {
                VarCatalogOptions opt = new VarCatalogOptions();
                opt.Quality = (QualityMode)comboBoxQualityMode.SelectedIndex;
                opt.VarIdPixels = (int)numericUpDownVarIdPix.Value;
                opt.VarNamePixels = (int)numericUpDownVarNamePix.Value;
                opt.CodeIdPixels = (int)numericUpDownCodeIdPix.Value;
                opt.CodeNamePixels = (int)numericUpDownCodeNamePix.Value;
                return opt;
            }
            set
            {
                InitControls(value);
            }
        }

        private void buttonLoadMap_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openFileDlg = new OpenFileDialog();
                openFileDlg.Filter = "Cfg |*.cfg|Allfiles|*.*";
                if (openFileDlg.ShowDialog() == DialogResult.OK)
                {
                    System.IO.TextReader reader = new System.IO.StreamReader(openFileDlg.FileName);
                    System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(VarCatalogOptions));
                    CatalogOptions = (VarCatalogOptions)serializer.Deserialize(reader);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonSaveMap_Click(object sender, EventArgs e)
        {
            try
            {
                SaveFileDialog saveFileDlg = new SaveFileDialog();
                saveFileDlg.Filter = "Cfg |*.cfg|Allfiles|*.*";
                if (saveFileDlg.ShowDialog() == DialogResult.OK)
                {
                    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(CatalogOptions.GetType());
                    System.Text.StringBuilder sb = new System.Text.StringBuilder(128);
                    System.IO.StringWriter sw = new System.IO.StringWriter(sb);
                    x.Serialize(sw, CatalogOptions);
                    using (System.IO.StreamWriter swFile = new System.IO.StreamWriter(saveFileDlg.FileName, false, Encoding.Default))
                    {
                        swFile.Write(sb.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(this, ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }
    }


}
